package com.dgut.legalservice.controller;

import com.dgut.legalservice.domain.Product;
import com.dgut.legalservice.service.ProductService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/product")
public class ProductController {


    @Autowired
    ProductService productService;

    /**
     * 添加产品
     * @param product
     */
    @RequestMapping("/add")
    public ModelAndView addProduct(Product product) throws Exception {
        ModelAndView mv = new ModelAndView();

        int i = productService.addProduct(product);
        List<Product> products = productService.findProduct();
        mv.addObject("products",products);

        mv.setViewName("/platform/product-list.html");

        return mv;
    }

    /**
     * 删除产品
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/delete/{id}")
    public ModelAndView deleteProduct(@PathVariable Integer id) throws Exception{

        int i = productService.deleteProduct(id);

        ModelAndView mv = new ModelAndView();
        List<Product> products = productService.findProduct();
        mv.addObject("products",products);
        mv.setViewName("/platform/product-list.html");

        return mv;
    }

    /**
     * 更新产品
     * @param product
     * @return
     */
    @RequestMapping("/update")
    public ModelAndView updateProduct(@ModelAttribute("abc")Product product){
        int i = productService.updateProduct(product);
        ModelAndView mv = new ModelAndView();
        List<Product> products = productService.findProduct();
        System.out.println("产品数量是");
        mv.setViewName("/platform/product-list.html");
        mv.addObject("products",products);

        return mv;
    }

    /**
     * 查找产品
     * @return
     */
    @RequestMapping("/find")
    public ModelAndView findProduct() {

        List<Product> products = productService.findProduct();
        ModelAndView mv = new ModelAndView();

        mv.setViewName("/platform/product-list.html");
        mv.addObject("products",products);

        return mv;
    }

    @ModelAttribute
    public void ModerA(Integer id,Map<String,Product> map){
        Product product = productService.findId(id);
        map.put("abc",product);
    }

    @RequestMapping("/test")
    public String test(){
        return "dasdaad";
    }
}
